package com.tcp;

import java.io.DataOutputStream;
import java.net.Socket;
import java.util.Scanner;

public class TCPClientTC {
    public static void main(String[] args) {
        // 实现TCP多发多收
        System.out.println("======客户端启动======");
        // 创建TCP客户端 java.net.Socket
        try(
                Socket socket = new Socket("127.0.0.1", 9999);
                // 获取特殊数据流
                DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
                // 注册扫描器
                Scanner sc = new Scanner(System.in)
        ) {
            while (true) {
                System.out.println("请输入：");
                String str = sc.nextLine();
                if ("exit".equals(str)) {
                    System.out.println("退出成功！");
                    break;
                }
                outputStream.writeUTF(str);
                outputStream.flush();
            }
        } catch (Exception e) {
            System.out.println("发送TCP数据失败！");
            e.printStackTrace();
        }

    }
}
